package com.ymatou.iapi.productquery.testcase;

import java.util.Date;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.ProductBriefDto;
import com.ymatou.iapi.productquery.service.GetProductBriefByProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.bean.ProductBean;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.mongo.jproduct.ProductTimeStampWapper;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_GetProductBriefByProductId {
	private static GetProductBriefByProductIdCall getProductBriefByProductIdCall = new GetProductBriefByProductIdCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品简要信息");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductBriefByProductIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_001() {
		Logger.start(true,"普通商品查询");
		try {
			String productId = ProductFactory.getOnlyProduct().getProductId();
			
			checkProductBrief(productId,0);
			
			checkProductBrief(productId,0,true);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_002() {
		Logger.start(true,"普通活动商品查询");
		try {
			ProductBean productBean = ProductFactory.getActivityProduct(sellerId,sellerName);
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity());
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity(),true);
			
			//活动库存清0;
			SellerProductCallService.updateActivityStockNumAs0(sellerId, productBean.getProductId());
			YMTDateUtil.waitTime(3);
			
			checkProductBrief(productBean.getProductId(),0);
			
			checkProductBrief(productBean.getProductId(),0,true);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_002_1() {
		Logger.start(true,"交易隔离活动商品查询");
		try {
			ProductBean productBean = ProductFactory.getActivityProduct(sellerId,sellerName);
			
			
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity());
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity(),true);
			
			//交易隔离
			if (YmtMongoDriverFactroy.IsNewJavaMongoOpen){
				new ActivityProductsWapper().update(productBean.getProductId(), MapUtil.hashMap("isolation",true));
				new ProductTimeStampWapper().update(productBean.getProductId(), MapUtil.hashMap("aut",new Date()));
			}
			
			
			
			
			YMTDateUtil.waitTime(3);
			
			checkProductBrief(productBean.getProductId(),0);
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity(),true);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_003() {
		Logger.start(true,"将来活动商品查询");
		try {
			ProductBean productBean = ProductFactory.getFutureActivityProduct(sellerId,sellerName,null);
			
			checkProductBrief(productBean.getProductId(),0);
			
			checkProductBrief(productBean.getProductId(),0,true);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_004() {
		Logger.start(true,"不存在商品查询");
		try {
			JSONObject ret = getProductBriefByProductIdCall.callAndGetReturnData("dddddddddddddd");
			
			Logger.verifyEquals(200, ret.getInt("Code"), "code");
			Logger.verifyEquals(true, ret.isNull("Data"), "Data is null");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductId_005() {
		Logger.start(true,"商品同时参加两个活动");
		try {
			ProductBean productBean = ProductFactory.getTwoSameTimeActivityProduct(sellerId, sellerName);
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowActivity());
			
			checkProductBrief(productBean.getProductId(),productBean.getProductActivitysBean().getNowAnotherActivity(),true);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	public static void checkProductBrief(String productId,int aid) throws Exception{
		
		checkProductBrief(productId,aid,false);
	}
	
	public static void checkProductBrief(String productId,int aid,boolean isTradeIsolation) throws Exception{
		JSONObject ret = getProductBriefByProductIdCall.callAndGetReturnData(productId,isTradeIsolation);
		
		if (ret.isNull("Data")){
			Logger.verifyEquals(false, true, "没有查到商品数据");
			return;
		}
		ProductBriefDto actBean = ProductQueryCallService.getBeanFromJson(ret.getJSONObject("Data"), ProductBriefDto.class);
		
		ProductBriefDto tgtBean = getProductBriefDto(productId, aid);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetProductBriefByProductId");
		
		
		
	}
	
	
	private static ProductBriefDto getProductBriefDto(String productId,int aid) throws Exception{
		ProductsWapper productsWapper = new ProductsWapper();
		Map<String,Object> productMap = productsWapper.getByProductId(productId);

		ProductBriefDto productBriefDto = new ProductBriefDto();
		productBriefDto.setProductId(productId);
		productBriefDto.setSellerId(Integer.parseInt(productMap.get("sid").toString()));
		productBriefDto.setSellerCountryId(Integer.parseInt(productMap.get("country").toString()));
		productBriefDto.setMasterCategoryId(Integer.parseInt(productMap.get("mcatid").toString()));
		productBriefDto.setCategoryId(Integer.parseInt(productMap.get("scatid").toString()));
		productBriefDto.setThirdCategoryId(Integer.parseInt(productMap.get("tcatid").toString()));
		productBriefDto.setIsPspProduct(Boolean.parseBoolean(productMap.get("ispsp").toString()));
		productBriefDto.setActivityId(aid);
		
		return productBriefDto;
		
	}
	
}
